package cn.donghuapian.component;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * @description:
 * @author: hzw
 * @time: 2024/11/7 15:24
 */
public class Pachong {

    public static void main(String[] args) {
        String url = "https://element.eleme.cn/#/zh-CN/component/color";

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建 GET 请求
            HttpGet request = new HttpGet(url);

            // 发送请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    // 将响应内容转换为字符串
                    String htmlContent = EntityUtils.toString(entity);

                    // 使用 Jsoup 解析 HTML
                    Document document = Jsoup.parse(htmlContent);

                    // 获取网页标题
                    String title = document.title();
                    System.out.println("Title: " + title);

                    // 获取所有链接
                    Elements links = document.select("a[href]");
                    for (Element link : links) {
                        System.out.println("Link: " + link.attr("href"));
                        System.out.println("Text: " + link.text());
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
